// ParticleEmitter.h

#ifndef _EFFECT_ParticleEmitter_H_
#define _EFFECT_ParticleEmitter_H_

#ifndef _CORE_std_H_
#include <core/std.h>
#endif

#ifndef _EFFECT_Particle_H_
#include <effect/Particle.h>
#endif

#include <gfx/Texture.h>

struct IRenderingEngine;

namespace Effect
{
#define MAXIMUM_UPDATE_RATE 90.0f

	class ParticleEmitter
	{
	public:
		ParticleEmitter();
		virtual ~ParticleEmitter();

		static ParticleEmitter* createFromFile(const char* pszFileName);

		void	renderParticles(IRenderingEngine *renderer);
		void	updateWithDelta(GLfloat fDelta);
		void	stopParticleEmitter(void);

		BOOL	addParticle(void);
		void	initParticle(Particle* particle);
		void	parseParticleConfig(CMarkup& xmlConfig);
		void	setupArrays(void);

		Math::vec2		GetSroucePosition(void) { return sourcePosition; }
		void			SetSourcePosition(Math::vec2 position) { sourcePosition = position; }

		GLint			GetParticleCount(void) { return particleCount; }
		void			SetParticleCount(GLint count) { particleCount = count; }

		BOOL			IsActive(void) { return active; }
		void			SetActive(BOOL bValue) { active = bValue; }

		GLfloat			GetDuration(void) { return duration; }
		void			SetDuration(GLfloat fDuration) { duration = fDuration; }

	protected:

	public:
		// Particle variables
		int				emitterType;
		GFX::Texture *	pTexture;
		Math::vec2		sourcePosition, sourcePositionVariance;
		GLfloat			angle, angleVariance;
		GLfloat			speed, speedVariance;
		GLfloat			radialAcceleration, tangentialAcceleration;
		GLfloat			radialAccelVariance, tangentialAccelVariance;
		Math::vec2		gravity;
		GLfloat			particleLifespan, particleLifespanVariance;
		Math::color4f	startColor, startColorVariance;
		Math::color4f	finishColor, finishColorVariance;
		GLfloat			startParticleSize, startParticleSizeVariance;
		GLfloat			finishParticleSize, finishParticleSizeVariance;
		GLuint			maxParticles;
		GLint			particleCount;
		GLfloat			emissionRate;
		GLfloat			emitCounter;
		GLfloat			elapsedTime;
		GLfloat			duration;
		GLfloat			rotationStart, rotationStartVariance;
		GLfloat			rotationEnd, rotationEndVariance;

		int				blendFuncSource, blendFuncDestination;

		GLfloat			maxRadius;				// Max radius at which particles are drawn when rotating
		GLfloat			maxRadiusVariance;		// Variance of the maxRadius
		GLfloat			radiusSpeed;			// The speed at which a particle moves from maxRadius to min Radius
		GLfloat			minRadius;				// Radius from source below which a particle dies
		GLfloat			rotatePerSecond;		// Number of degrees to rotate a particle around the source pos per second
		GLfloat			rotatePerSecondVariance; // Variance in degrees for rotatePerSecond

		// Particle Emitter variables
		BOOL			active;
		BOOL			useTexture;
		GLint			particleIndex;	// Stores the number of particles that are going to be rendered
		GLint			vertexIndex; // Stores the index of the vertices being used for each particle

		// Render
		GLuint			verticesID;		// Holds the buffer name of the VBO that stores the color and vertices info for the particles
		GLuint			vertexObjectID;
		Particle *		pParticles;	// Array of particles that hold the particle emitters particle details
		ParticleQuad *	pQuads;		// Array holding quad information for each particle;
		GLushort *		pIndices;	// Array holding an index reference into an array of quads for rendering
	};
};

#endif // _EFFECT_ParticleEmitter_H_